package com.base.mybaseproject.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import com.base.mybaseproject.base.Constant;
import com.support.Utils;

public class HttpUtils {

	public static JSONObject post(String URL, List<NameValuePair> params) {
		try {
			URL = Constant.BASE_URL + URL;
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost request = new HttpPost(URL);

			request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

			httpClient.getParams().setParameter(org.apache.http.params.CoreConnectionPNames.CONNECTION_TIMEOUT,
					10 * 1000);
			httpClient.getParams().setParameter(org.apache.http.params.CoreConnectionPNames.SO_TIMEOUT, 10 * 1000);
			Utils.d("HTTP_start", URL + "-->" + params.toString());
			HttpResponse response = httpClient.execute(request);

			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 连接并响应成
				String responseStr = EntityUtils.toString(response.getEntity());
				JSONObject result = new JSONObject(responseStr);
				Utils.d("HTTP_over", result + "");

				return result;
			} else {
				return null;
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
}
